package com.xie.smfs.event;

import com.xie.smfs.registry.ModItems;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.loot.v2.LootTableEvents;
import net.minecraft.item.Item;
import net.minecraft.loot.LootPool;
import net.minecraft.loot.LootTable;
import net.minecraft.loot.LootTables;
import net.minecraft.loot.entry.ItemEntry;
import net.minecraft.loot.provider.number.ConstantLootNumberProvider;
import net.minecraft.loot.condition.RandomChanceLootCondition;
import net.minecraft.util.Identifier;

public class ModLootTableEvents {

    public static void register() {
        LootTableEvents.MODIFY.register((resourceManager, lootManager, id, tableBuilder, source) -> {
            if (source.isBuiltin()) {
                if (LootTables.ABANDONED_MINESHAFT_CHEST.equals(id)) {
                addItemToLootTable(tableBuilder, ModItems.RED_GHOST_CANDLE, 0.1f);
            }
            if (LootTables.JUNGLE_TEMPLE_CHEST.equals(id) || LootTables.DESERT_PYRAMID_CHEST.equals(id)) {
                addItemToLootTable(tableBuilder, ModItems.RED_GHOST_CANDLE, 0.1f);
            }
            if (LootTables.SIMPLE_DUNGEON_CHEST.equals(id)) {
                addItemToLootTable(tableBuilder, ModItems.RED_GHOST_CANDLE, 0.5f);
                addItemToLootTable(tableBuilder, ModItems.COFFIN_NAIL, 0.3f);
            }
            if (LootTables.BURIED_TREASURE_CHEST.equals(id)) {
                addItemToLootTable(tableBuilder, ModItems.RED_GHOST_CANDLE, 0.2f);
            }
            }
        });

        ServerLifecycleEvents.SERVER_STARTED.register(server -> {
            // 初始化逻辑（如果有）
        });
    }

    private static void addItemToLootTable(LootTable.Builder tableBuilder, Item item, float chance) {
        LootPool.Builder poolBuilder = LootPool.builder()
                .rolls(ConstantLootNumberProvider.create(1))
                .conditionally(RandomChanceLootCondition.builder(chance).build())
                .with(ItemEntry.builder(item));

        tableBuilder.pool(poolBuilder);
    }
}